package cn.wolfcode.trip.base.util;

import cn.wolfcode.trip.base.domain.User;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

//用户操作相关的工具类
public class UserContext {
    public static final String USER_IN_SESSION="USER_IN_SESSION";

    private UserContext(){}
    //获取HttpSession
    public static HttpSession getSession(){
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)attributes;
        return requestAttributes.getRequest().getSession();
    }
    //设置当前登录用户
    public static void setCurrentUser(User emp){
        getSession().setAttribute(USER_IN_SESSION,emp);
    }
    //获取当前登录用户
    public static User getCurrentUser(){
        return (User) getSession().getAttribute(USER_IN_SESSION);
    }


}
